package com.fc.v2.service;

import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class StatisticsSpatialService {
    public Map<Object,Object> containsKey(List<Map<Object,Object>> values,Object key){
        for (int i = 0; i < values.size(); i++) {
            if(values.get(i).containsKey(key)){
                return values.get(i);
            }
        }
        return null;
    }
    public Map<Object,Object> containsValue(List<Map<Object,Object>> values,Object val){
        for (int i = 0; i < values.size(); i++) {
            for (Object obj : values.get(i).values()) {
                if(isNumber(obj) && isNumber(val)){
                    if(Double.parseDouble(obj.toString()) - Double.parseDouble(val.toString()) == 0){
                        return values.get(i);
                    }
                }
                else{
                    if((val == null && obj == null) || val.toString().equalsIgnoreCase(val.toString())){
                        return values.get(i);
                    }
                }
            }
        }
        return null;
    }

    public boolean isNumber (Object obj) {
        if (obj instanceof Number) {
            return true;
        } else if (obj instanceof String){
            try{
                Double.parseDouble((String)obj);
                return true;
            }catch (Exception e) {
                return false;
            }
        }
        return false;
    }
}
